package cn.edu.guet.tts.controller;

import cn.edu.guet.tts.common.Result;
import cn.edu.guet.tts.entity.TbStation;
import cn.edu.guet.tts.entity.TbTrain;
import cn.edu.guet.tts.service.ITbStationService;
import cn.edu.guet.tts.service.ITbTrainService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;

@RestController
@RequestMapping("/tbStation")
@Slf4j
public class TbStationController {

    @Resource
    private ITbStationService tbStationService;
    @Resource
    private ITbTrainService tbTrainService;

    /**
     * 新增或者更新车站
     * @param tbStation
     * @return
     */
    @PostMapping()
    public Result save(@RequestBody TbStation tbStation) {
        log.info("新增或修改车站信息：{}", tbStation);
        return Result.success(tbStationService.saveOrUpdate(tbStation));
    }

    /**
     * 根据车站id删除
     * @param id
     * @return
     */
    @Transactional
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id获取车站名：{}", id);
        String stationName = tbStationService.getById(id).getStationName();
        List<TbTrain> list = tbTrainService.list();
        for(TbTrain train:list){
            if (train.getStartStation().equals(stationName) || train.getEndStation().equals(stationName)) {
                log.info("该车站下还有车次，无法删除：{}", id);
                return Result.error("400","该车站下还有车次，无法删除");
            }
        }
        log.info("删除车站信息：{}", id);
        return Result.success(tbStationService.removeById(id));
    }

    /**
     * 根据车票id批量删除
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        log.info("批量删除车站信息：{}", ids);
        ids.forEach(this::delete);
        return Result.success(tbStationService.removeByIds(ids));
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping
    public Result findAll() {
        log.info("查询全部车站信息");
        return Result.success(tbStationService.list());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        log.info("根据车站id查询车站信息：{}", id);
        return Result.success(tbStationService.getById(id));
    }

    /**
     * 分页
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<TbStation> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("station_name", name);
        }
        log.info("查询第：{}"+"页，每页：{}"+"条数据", pageNum, pageSize);
        return Result.success(tbStationService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}
